﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class 自定义分页 : System.Web.UI.Page
{
    private string connstr;
    private string sql;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridBind();

        }
      
    }
    protected void GridBind() //构造函数
    {
        connstr = ConfigurationManager.AppSettings["SqlConn"];
        sql = "Select * from zhuanti";
        SqlConnection MyConn = new SqlConnection(connstr);
        DataSet ds = new DataSet();
        SqlDataAdapter MyDa = new SqlDataAdapter(sql, MyConn);
        MyDa.Fill(ds, "cmd");
        Fenye.DataSource = ds.Tables["cmd"].DefaultView;
        Fenye.DataBind();
    }
    protected void PageButtonClick(object sender, EventArgs e)
        //构造分页函数
    {
        string arg = ((LinkButton)sender).CommandArgument.ToString();
        switch (arg)
        {
            default:
                Fenye.CurrentPageIndex = 0;
                break;
            case "prev"://前一頁
                if (Fenye.CurrentPageIndex > 1)
                {
                    Fenye.CurrentPageIndex -= 1;
                }
                break;
            case "next"://下一頁
                if (Fenye.CurrentPageIndex < (Fenye.PageCount - 1))
                {
                    Fenye.CurrentPageIndex += 1;
                }
                break;

            case "last":

                Fenye.CurrentPageIndex = Fenye.PageCount - 1;
                break;
               
        }
        GridBind();
        Response.Write("当前页 : " + Fenye.CurrentPageIndex);
        ShowList();

    }
    public string formatText(string tt)//
    {
        if (tt.Length >= 100)
        {
            return (tt.Substring(0,120) + "...");
        }
        else
        {
            return (tt);
           
        }
    }

    private void ShowList()
    {
        int 总页数, 当前页,总记录;
        connstr = ConfigurationManager.AppSettings["SqlConn"];
        sql = "Select * from zhuanti";
        SqlConnection MyConn = new SqlConnection(connstr);
        DataSet ds = new DataSet();
        SqlDataAdapter MyDa = new SqlDataAdapter(sql, MyConn);
        MyDa.Fill(ds, "cmd");
        总记录=ds.Tables["cmd"].Rows.Count;
        总页数=总记录/1;
        当前页 = Fenye.CurrentPageIndex;
        MyConn.Close();
        Ccount.Text = "总记录: " + 总记录 + "&nbsp;&nbsp;&nbsp;第" + 总页数 + "/"
                       +当前页+"页";
    }
}